<?php

namespace App\Http\Controllers\Api;

use App\Models\Activity;
use App\Models\Banner;
use App\Models\Book;
use App\Models\BookList;
use App\Models\Category;
use App\Models\HomepageRecommendModule;
use App\Models\HotKeyword;
use App\Models\User;
use App\Models\UserFavoriteTag;
use App\Models\UserSearchKeyword;
use Illuminate\Http\Request;
use Cache;

class HomepageController extends BaseController
{
    public function index()
    {
        //图书分类
        $categoryModel = new Category();
        $categories_arr = $categoryModel->getParentCategories();
        $result['categories'] = $categories_arr;

        //热门活动
        $result['hot_activities'] = [
            'items1' => [],
            'items2' => [],
        ];

        $activity1 = Banner::query()->where('position',1)->first();
        if(!empty($activity1))
        {
            array_push($result['hot_activities']['items2'],['id'=>$activity1->id,'position' => $activity1->position,'url'=>$activity1->url,'image'=>$activity1->image]);
        }

        $activity2 = Banner::query()->whereIn('position',[2,3])->orderBy('id','asc')->limit(2)->get();
        if(!empty($activity2))
        {
            foreach ($activity2 as $a)
            {
                array_push($result['hot_activities']['items2'],['id' => $a->id,'position' => $a->position,'url' => $a->url,'image' => $a->image]);
            }
        }

        //书单
        $result['book_lists'] = [];

        $book_lists = BookList::query()->with(['books'=>function($query){
            $query->select(['books.id as book_id']);
        }])->where('is_homepage',1)->where('deleted',0)->orderBy('sort','asc')->inRandomOrder()->limit(3)->get();
        if(!empty($book_lists))
        {
            foreach ($book_lists as $v)
            {
                $result['book_lists'][] = [
                    'id'         => $v->id,
                    'title'      => $v->title,
                    'list_image' => $v->list_image,
                    'book_num'   => count($v->books)
                ];
            }
        }


        //获取推荐模块
        $homepageRecommendModuleModel = new HomepageRecommendModule();
        $modules = $homepageRecommendModuleModel->getHomepageRecommendModules();
        $result['modules'] = $modules;


        return $this->res([
            'data' => $result
        ]);
    }

    /**
     * 猜你喜欢
     */
    private function _books()
    {
        $result = [];
        //猜你喜欢
        $columns = ['id','title','image','score','total_stock','author','old_price','new_book_price','new_book_stock','good_book_price','good_book_stock','medium_book_price','medium_book_stock','state','homepage_recommend_module_ids','medium_book_price2','medium_book_stock2','medium_book_price3','medium_book_stock3','medium_book_price4','medium_book_stock4','medium_book_price5','medium_book_stock5','medium_book_price6','medium_book_stock6','medium_book_discount','good_book_discount','new_book_discount','medium_book_discount2','medium_book_discount3','medium_book_discount4','medium_book_discount5','medium_book_discount6'];
        $tags = \DB::table('user_favorite_tags')->where('user_id',$this->user_id)->get();

        if($tags->isNotEmpty())
        {
            //dd($tags);
            $cate_ids = [];
            $cate_pids = [];
            foreach ($tags as $tag)
            {
                $cate_ids[] = $tag->category_id;
                $cate_pids[] = $tag->category_pid;
            }

            $book_ids = [];
            $relations = \DB::table('book_category_relations')->whereIn('category_id',$cate_ids)->get();
            if($relations->isNotEmpty())
            {
                foreach ($relations as $r)
                {
                    $book_ids[] = $r->book_id;
                }
            }

            $list = Book::query()->select($columns)->where('total_stock','>',0)->where('deleted',0)->where('state',1)->whereIn('id',$book_ids)->inRandomOrder()->limit(100)->get();

            if($list->isNotEmpty())
            {
                foreach ($list as $item)
                {
                    $stock_arr = [];
                    $discount_arr = [];
                    $price_arr = [];

                    $discount_arr2 = [];
                    $price_arr2 = [];

                    if($item->new_book_stock > 0)
                    {
                        $stock_arr[] = $item->new_book_stock;
                        $discount_arr2[] = $item->new_book_discount;
                        $price_arr2[] = $item->new_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $item->new_book_discount;
                        $price_arr[] = $item->new_book_price;
                    }

                    if($item->good_book_stock > 0)
                    {
                        $stock_arr[] = $item->good_book_stock;
                        $discount_arr2[] = $item->good_book_discount;
                        $price_arr2[] = $item->good_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $item->good_book_discount;
                        $price_arr[] = $item->good_book_price;
                    }

                    if($item->medium_book_stock > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock;
                        $discount_arr2[] = $item->medium_book_discount;
                        $price_arr2[] = $item->medium_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount;
                        $price_arr[] = $item->medium_book_price;
                    }

                    if($item->medium_book_stock2 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock2;
                        $discount_arr2[] = $item->medium_book_discount2;
                        $price_arr2[] = $item->medium_book_price2;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount2;
                        $price_arr[] = $item->medium_book_price2;
                    }

                    if($item->medium_book_stock3 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock3;
                        $discount_arr2[] = $item->medium_book_discount3;
                        $price_arr2[] = $item->medium_book_price3;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount3;
                        $price_arr[] = $item->medium_book_price3;
                    }

                    if($item->medium_book_stock4 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock4;
                        $discount_arr2[] = $item->medium_book_discount4;
                        $price_arr2[] = $item->medium_book_price4;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount4;
                        $price_arr[] = $item->medium_book_price4;
                    }

                    if($item->medium_book_stock5 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock5;
                        $discount_arr2[] = $item->medium_book_discount5;
                        $price_arr2[] = $item->medium_book_price5;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount5;
                        $price_arr[] = $item->medium_book_price5;
                    }

                    if($item->medium_book_stock6 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock6;
                        $discount_arr2[] = $item->medium_book_discount6;
                        $price_arr2[] = $item->medium_book_price6;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount6;
                        $price_arr[] = $item->medium_book_price6;
                    }

                    $is_more = 0;
                    if(count($stock_arr) > 1)
                    {
                        $is_more = 1;
                    }

                    $min_discount = 0;
                    $min_price = 0.0;

                    if(count($stock_arr) == 0)
                    {
                        if(!empty($discount_arr))
                        {
                            $min_discount = min($discount_arr);
                        }
                        if(!empty($price_arr))
                        {
                            $min_price = min($price_arr);
                        }
                    }
                    else
                    {
                        if(!empty($discount_arr2))
                        {
                            $min_discount = min($discount_arr2);
                        }
                        if(!empty($price_arr2))
                        {
                            $min_price = min($price_arr2);
                        }
                    }

                    $result[] = [
                        'id'                             => $item->id,
                        'title'                          => $item->title,
                        'author'                         => $item->author,
                        'image'                          => $item->image,
                        'score'                          => $item->score,
                        'total_stock'                    => $item->total_stock,
                        'new_book_price'                 => $item->new_book_price,
                        'new_book_stock'                 => $item->new_book_stock,
                        'good_book_price'                => $item->good_book_price,
                        'good_book_stock'                => $item->good_book_stock,
                        'medium_book_price'              => $item->medium_book_price,
                        'medium_book_stock'              => $item->medium_book_stock,
                        'medium_book_price2'             => $item->medium_book_price2,
                        'medium_book_stock2'             => $item->medium_book_stock2,
                        'medium_book_price3'             => $item->medium_book_price3,
                        'medium_book_stock3'             => $item->medium_book_stock3,
                        'medium_book_price4'             => $item->medium_book_price4,
                        'medium_book_stock4'             => $item->medium_book_stock4,
                        'medium_book_price5'             => $item->medium_book_price5,
                        'medium_book_stock5'             => $item->medium_book_stock5,
                        'medium_book_price6'             => $item->medium_book_price6,
                        'medium_book_stock6'             => $item->medium_book_stock6,
                        'medium_book_discount'           => $item->medium_book_discount,
                        'new_book_discount'              => $item->new_book_discount,
                        'good_book_discount'             => $item->good_book_discount,
                        'medium_book_discount2'          => $item->medium_book_discount2,
                        'medium_book_discount3'          => $item->medium_book_discount3,
                        'medium_book_discount4'          => $item->medium_book_discount4,
                        'medium_book_discount5'          => $item->medium_book_discount5,
                        'medium_book_discount6'          => $item->medium_book_discount6,
                        'state'                          => $item->state,
                        'old_price'                      => $item->old_price,
                        'homepage_recommend_module_ids'  => $item->homepage_recommend_module_ids,
                        'is_more'                        => $is_more,
                        'min_discount'                   => $min_discount,
                        'min_price'                      => $min_price,
                    ];
                }
            }

            $count = count($result);
            if($count < 100)
            {
                $limit = 100 - $count;
                $limit_list = Book::query()->select($columns)->where('total_stock','>',0)->where('deleted',0)->where('state',1)->whereIn('category_pid',$cate_pids)->whereNotIn('id',$book_ids)->inRandomOrder()->limit($limit)->get();
                if($limit_list->isNotEmpty())
                {
                    foreach ($limit_list as $item)
                    {
                        $stock_arr = [];
                        $discount_arr = [];
                        $price_arr = [];

                        $discount_arr2 = [];
                        $price_arr2 = [];

                        if($item->new_book_stock > 0)
                        {
                            $stock_arr[] = $item->new_book_stock;
                            $discount_arr2[] = $item->new_book_discount;
                            $price_arr2[] = $item->new_book_price;
                        }
                        else
                        {
                            $discount_arr[] = $item->new_book_discount;
                            $price_arr[] = $item->new_book_price;
                        }

                        if($item->good_book_stock > 0)
                        {
                            $stock_arr[] = $item->good_book_stock;
                            $discount_arr2[] = $item->good_book_discount;
                            $price_arr2[] = $item->good_book_price;
                        }
                        else
                        {
                            $discount_arr[] = $item->good_book_discount;
                            $price_arr[] = $item->good_book_price;
                        }

                        if($item->medium_book_stock > 0)
                        {
                            $stock_arr[] = $item->medium_book_stock;
                            $discount_arr2[] = $item->medium_book_discount;
                            $price_arr2[] = $item->medium_book_price;
                        }
                        else
                        {
                            $discount_arr[] = $item->medium_book_discount;
                            $price_arr[] = $item->medium_book_price;
                        }

                        if($item->medium_book_stock2 > 0)
                        {
                            $stock_arr[] = $item->medium_book_stock2;
                            $discount_arr2[] = $item->medium_book_discount2;
                            $price_arr2[] = $item->medium_book_price2;
                        }
                        else
                        {
                            $discount_arr[] = $item->medium_book_discount2;
                            $price_arr[] = $item->medium_book_price2;
                        }

                        if($item->medium_book_stock3 > 0)
                        {
                            $stock_arr[] = $item->medium_book_stock3;
                            $discount_arr2[] = $item->medium_book_discount3;
                            $price_arr2[] = $item->medium_book_price3;
                        }
                        else
                        {
                            $discount_arr[] = $item->medium_book_discount3;
                            $price_arr[] = $item->medium_book_price3;
                        }

                        if($item->medium_book_stock4 > 0)
                        {
                            $stock_arr[] = $item->medium_book_stock4;
                            $discount_arr2[] = $item->medium_book_discount4;
                            $price_arr2[] = $item->medium_book_price4;
                        }
                        else
                        {
                            $discount_arr[] = $item->medium_book_discount4;
                            $price_arr[] = $item->medium_book_price4;
                        }

                        if($item->medium_book_stock5 > 0)
                        {
                            $stock_arr[] = $item->medium_book_stock5;
                            $discount_arr2[] = $item->medium_book_discount5;
                            $price_arr2[] = $item->medium_book_price5;
                        }
                        else
                        {
                            $discount_arr[] = $item->medium_book_discount5;
                            $price_arr[] = $item->medium_book_price5;
                        }

                        if($item->medium_book_stock6 > 0)
                        {
                            $stock_arr[] = $item->medium_book_stock6;
                            $discount_arr2[] = $item->medium_book_discount6;
                            $price_arr2[] = $item->medium_book_price6;
                        }
                        else
                        {
                            $discount_arr[] = $item->medium_book_discount6;
                            $price_arr[] = $item->medium_book_price6;
                        }

                        $is_more = 0;
                        if(count($stock_arr) > 1)
                        {
                            $is_more = 1;
                        }

                        $min_discount = 0;
                        $min_price = 0.0;

                        if(count($stock_arr) == 0)
                        {
                            if(!empty($discount_arr))
                            {
                                $min_discount = min($discount_arr);
                            }

                            if(!empty($price_arr))
                            {
                                $min_price = min($price_arr);
                            }
                        }
                        else
                        {
                            if(!empty($discount_arr2))
                            {
                                $min_discount = min($discount_arr2);
                            }

                            if(!empty($price_arr2))
                            {
                                $min_price = min($price_arr2);
                            }
                        }

                        $result[] = [
                            'id'                             => $item->id,
                            'title'                          => $item->title,
                            'author'                         => $item->author,
                            'image'                          => $item->image,
                            'score'                          => $item->score,
                            'total_stock'                    => $item->total_stock,
                            'medium_book_price'              => $item->medium_book_price,
                            'medium_book_stock'              => $item->medium_book_stock,
                            'medium_book_price2'             => $item->medium_book_price2,
                            'medium_book_stock2'             => $item->medium_book_stock2,
                            'medium_book_price3'             => $item->medium_book_price3,
                            'medium_book_stock3'             => $item->medium_book_stock3,
                            'medium_book_price4'             => $item->medium_book_price4,
                            'medium_book_stock4'             => $item->medium_book_stock4,
                            'medium_book_price5'             => $item->medium_book_price5,
                            'medium_book_stock5'             => $item->medium_book_stock5,
                            'medium_book_price6'             => $item->medium_book_price6,
                            'medium_book_stock6'             => $item->medium_book_stock6,
                            'state'                          => $item->state,
                            'old_price'                      => $item->old_price,
                            'new_book_price'                 => $item->new_book_price,
                            'new_book_stock'                 => $item->new_book_stock,
                            'good_book_price'                => $item->good_book_price,
                            'good_book_stock'                => $item->good_book_stock,
                            'medium_book_discount'           => $item->medium_book_discount,
                            'new_book_discount'              => $item->new_book_discount,
                            'good_book_discount'             => $item->good_book_discount,
                            'medium_book_discount2'          => $item->medium_book_discount2,
                            'medium_book_discount3'          => $item->medium_book_discount3,
                            'medium_book_discount4'          => $item->medium_book_discount4,
                            'medium_book_discount5'          => $item->medium_book_discount5,
                            'medium_book_discount6'          => $item->medium_book_discount6,
                            'homepage_recommend_module_ids'  => $item->homepage_recommend_module_ids,
                            'is_more'                        => $is_more,
                            'min_discount'                   => $min_discount,
                            'min_price'                      => $min_price,
                        ];
                    }
                }
            }

        }
        else
        {
            $list = Book::query()->select($columns)->where('total_stock','>',0)->where('deleted',0)->where('state',1)->inRandomOrder()->limit(100)->get();
            if($list->isNotEmpty())
            {
                foreach ($list as $item)
                {
                    $stock_arr = [];
                    $discount_arr = [];
                    $price_arr = [];

                    $discount_arr2 = [];
                    $price_arr2 = [];

                    if($item->new_book_stock > 0)
                    {
                        $stock_arr[] = $item->new_book_stock;
                        $discount_arr2[] = $item->new_book_discount;
                        $price_arr2[] = $item->new_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $item->new_book_discount;
                        $price_arr[] = $item->new_book_price;
                    }

                    if($item->good_book_stock > 0)
                    {
                        $stock_arr[] = $item->good_book_stock;
                        $discount_arr2[] = $item->good_book_discount;
                        $price_arr2[] = $item->good_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $item->good_book_discount;
                        $price_arr[] = $item->good_book_price;
                    }

                    if($item->medium_book_stock > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock;
                        $discount_arr2[] = $item->medium_book_discount;
                        $price_arr2[] = $item->medium_book_price;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount;
                        $price_arr[] = $item->medium_book_price;
                    }

                    if($item->medium_book_stock2 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock2;
                        $discount_arr2[] = $item->medium_book_discount2;
                        $price_arr2[] = $item->medium_book_price2;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount2;
                        $price_arr[] = $item->medium_book_price2;
                    }

                    if($item->medium_book_stock3 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock3;
                        $discount_arr2[] = $item->medium_book_discount3;
                        $price_arr2[] = $item->medium_book_price3;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount3;
                        $price_arr[] = $item->medium_book_price3;
                    }

                    if($item->medium_book_stock4 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock4;
                        $discount_arr2[] = $item->medium_book_discount4;
                        $price_arr2[] = $item->medium_book_price4;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount4;
                        $price_arr[] = $item->medium_book_price4;
                    }

                    if($item->medium_book_stock5 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock5;
                        $discount_arr2[] = $item->medium_book_discount5;
                        $price_arr2[] = $item->medium_book_price5;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount5;
                        $price_arr[] = $item->medium_book_price5;
                    }

                    if($item->medium_book_stock6 > 0)
                    {
                        $stock_arr[] = $item->medium_book_stock6;
                        $discount_arr2[] = $item->medium_book_discount6;
                        $price_arr2[] = $item->medium_book_price6;
                    }
                    else
                    {
                        $discount_arr[] = $item->medium_book_discount6;
                        $price_arr[] = $item->medium_book_price6;
                    }

                    $is_more = 0;
                    if(count($stock_arr) > 1)
                    {
                        $is_more = 1;
                    }

                    $min_discount = 0;
                    $min_price = 0.0;

                    if(count($stock_arr) == 0)
                    {
                        if(!empty($discount_arr))
                        {
                            $min_discount = min($discount_arr);
                        }

                        if(!empty($price_arr))
                        {
                            $min_price = min($price_arr);
                        }
                    }
                    else
                    {
                        if(!empty($discount_arr2))
                        {
                            $min_discount = min($discount_arr2);
                        }

                        if(!empty($price_arr2))
                        {
                            $min_price = min($price_arr2);
                        }
                    }

                    $result[] = [
                        'id'                             => $item->id,
                        'title'                          => $item->title,
                        'author'                         => $item->author,
                        'image'                          => $item->image,
                        'score'                          => $item->score,
                        'total_stock'                    => $item->total_stock,
                        'medium_book_price'              => $item->medium_book_price,
                        'medium_book_stock'              => $item->medium_book_stock,
                        'medium_book_price2'             => $item->medium_book_price2,
                        'medium_book_stock2'             => $item->medium_book_stock2,
                        'medium_book_price3'             => $item->medium_book_price3,
                        'medium_book_stock3'             => $item->medium_book_stock3,
                        'medium_book_price4'             => $item->medium_book_price4,
                        'medium_book_stock4'             => $item->medium_book_stock4,
                        'medium_book_price5'             => $item->medium_book_price5,
                        'medium_book_stock5'             => $item->medium_book_stock5,
                        'medium_book_price6'             => $item->medium_book_price6,
                        'medium_book_stock6'             => $item->medium_book_stock6,
                        'state'                          => $item->state,
                        'old_price'                      => $item->old_price,
                        'new_book_price'                 => $item->new_book_price,
                        'new_book_stock'                 => $item->new_book_stock,
                        'good_book_price'                => $item->good_book_price,
                        'good_book_stock'                => $item->good_book_stock,
                        'medium_book_discount'           => $item->medium_book_discount,
                        'new_book_discount'              => $item->new_book_discount,
                        'good_book_discount'             => $item->good_book_discount,
                        'medium_book_discount2'          => $item->medium_book_discount2,
                        'medium_book_discount3'          => $item->medium_book_discount3,
                        'medium_book_discount4'          => $item->medium_book_discount4,
                        'medium_book_discount5'          => $item->medium_book_discount5,
                        'medium_book_discount6'          => $item->medium_book_discount6,
                        'homepage_recommend_module_ids'  => $item->homepage_recommend_module_ids,
                        'is_more'                        => $is_more,
                        'min_discount'                   => $min_discount,
                        'min_price'                      => $min_price,
                    ];
                }
            }

        }

        return $result;
    }

    public function books(Request $request)
    {
        $module_id = intval($request->get('module_id',1));
        $page      = intval($request->get('page',1));
        $page      = max($page ,1);
        $page_size = intval($request->get('page_size',10));

        if($module_id == -1)
        {
            $items = $this->_books();
            $data = [
                '_meta' => [
                    'total_count'  => 200,
                    'page_count'   => 1,
                    'current_page' => 1,
                    'per_page'     => 200,
                ],
                "items" => $items
            ];

            return $this->res([
                'data' => $data
            ]);
        }

        $builder = Book::query()->with(['homepage_recommend_modules'=>function($query) use ($module_id){
            $query->where('homepage_recommend_module_id',$module_id);
        }])->where('deleted',0)
            ->where('state',1);

        $builder->where(function ($query) use ($module_id) {
            $query->whereRaw("FIND_IN_SET($module_id,homepage_recommend_module_ids)");
        });

        switch ($module_id)
        {
            case 3:
                $builder->orderBy('score','desc');
                break;
            default:
                $builder->orderBy('total_stock','desc');
                break;
        }

        $columns = ['id','title','image','score','total_stock','author','old_price','medium_book_price','medium_book_stock','state','homepage_recommend_module_ids','new_book_price','new_book_stock','good_book_price','good_book_stock','medium_book_price2','medium_book_stock2','medium_book_price3','medium_book_stock3','medium_book_price4','medium_book_stock4','medium_book_price5','medium_book_stock5','medium_book_price6','medium_book_stock6','medium_book_discount','good_book_discount','new_book_discount','medium_book_discount2','medium_book_discount3','medium_book_discount4','medium_book_discount5','medium_book_discount6'];
        $list = $builder->paginate($page_size,$columns,'page',$page);
        $items = [];
        if(!empty($list->items()))
        {
            foreach ($list->items() as $item)
            {
                $stock_arr = [];
                $discount_arr = [];
                $price_arr = [];

                $discount_arr2 = [];
                $price_arr2 = [];

                if($item->new_book_stock > 0)
                {
                    $stock_arr[] = $item->new_book_stock;
                    $discount_arr2[] = $item->new_book_discount;
                    $price_arr2[] = $item->new_book_price;
                }
                else
                {
                    $discount_arr[] = $item->new_book_discount;
                    $price_arr[] = $item->new_book_price;
                }

                if($item->good_book_stock > 0)
                {
                    $stock_arr[] = $item->good_book_stock;
                    $discount_arr2[] = $item->good_book_discount;
                    $price_arr2[] = $item->good_book_price;
                }
                else
                {
                    $discount_arr[] = $item->good_book_discount;
                    $price_arr[] = $item->good_book_price;
                }

                if($item->medium_book_stock > 0)
                {
                    $stock_arr[] = $item->medium_book_stock;
                    $discount_arr2[] = $item->medium_book_discount;
                    $price_arr2[] = $item->medium_book_price;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount;
                    $price_arr[] = $item->medium_book_price;
                }

                if($item->medium_book_stock2 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock2;
                    $discount_arr2[] = $item->medium_book_discount2;
                    $price_arr2[] = $item->medium_book_price2;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount2;
                    $price_arr[] = $item->medium_book_price2;
                }

                if($item->medium_book_stock3 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock3;
                    $discount_arr2[] = $item->medium_book_discount3;
                    $price_arr2[] = $item->medium_book_price3;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount3;
                    $price_arr[] = $item->medium_book_price3;
                }

                if($item->medium_book_stock4 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock4;
                    $discount_arr2[] = $item->medium_book_discount4;
                    $price_arr2[] = $item->medium_book_price4;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount4;
                    $price_arr[] = $item->medium_book_price4;
                }

                if($item->medium_book_stock5 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock5;
                    $discount_arr2[] = $item->medium_book_discount5;
                    $price_arr2[] = $item->medium_book_price5;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount5;
                    $price_arr[] = $item->medium_book_price5;
                }

                if($item->medium_book_stock6 > 0)
                {
                    $stock_arr[] = $item->medium_book_stock6;
                    $discount_arr2[] = $item->medium_book_discount6;
                    $price_arr2[] = $item->medium_book_price6;
                }
                else
                {
                    $discount_arr[] = $item->medium_book_discount6;
                    $price_arr[] = $item->medium_book_price6;
                }

                $is_more = 0;
                if(count($stock_arr) > 1)
                {
                    $is_more = 1;
                }

                $min_discount = 0;
                $min_price = 0.0;

                if(count($stock_arr) == 0)
                {
                    if(!empty($discount_arr))
                    {
                        $min_discount = min($discount_arr);
                    }

                    if(!empty($price_arr))
                    {
                        $min_price = min($price_arr);
                    }
                }
                else
                {
                    if(!empty($discount_arr2))
                    {
                        $min_discount = min($discount_arr2);
                    }

                    if(!empty($price_arr2))
                    {
                        $min_price = min($price_arr2);
                    }
                }



                $items[] = [
                    'id'                             => $item->id,
                    'title'                          => $item->title,
                    'author'                         => $item->author,
                    'image'                          => $item->image,
                    'score'                          => $item->score,
                    'total_stock'                    => $item->total_stock,
                    'medium_book_price'              => $item->medium_book_price,
                    'medium_book_stock'              => $item->medium_book_stock,
                    'medium_book_price2'             => $item->medium_book_price2,
                    'medium_book_stock2'             => $item->medium_book_stock2,
                    'medium_book_price3'             => $item->medium_book_price3,
                    'medium_book_stock3'             => $item->medium_book_stock3,
                    'medium_book_price4'             => $item->medium_book_price4,
                    'medium_book_stock4'             => $item->medium_book_stock4,
                    'medium_book_price5'             => $item->medium_book_price5,
                    'medium_book_stock5'             => $item->medium_book_stock5,
                    'medium_book_price6'             => $item->medium_book_price6,
                    'medium_book_stock6'             => $item->medium_book_stock6,
                    'state'                          => $item->state,
                    'old_price'                      => $item->old_price,
                    'new_book_price'                 => $item->new_book_price,
                    'new_book_stock'                 => $item->new_book_stock,
                    'good_book_price'                => $item->good_book_price,
                    'good_book_stock'                => $item->good_book_stock,
                    'medium_book_discount'           => $item->medium_book_discount,
                    'new_book_discount'              => $item->new_book_discount,
                    'good_book_discount'             => $item->good_book_discount,
                    'medium_book_discount2'          => $item->medium_book_discount2,
                    'medium_book_discount3'          => $item->medium_book_discount3,
                    'medium_book_discount4'          => $item->medium_book_discount4,
                    'medium_book_discount5'          => $item->medium_book_discount5,
                    'medium_book_discount6'          => $item->medium_book_discount6,
                    'homepage_recommend_module_ids'  => $item->homepage_recommend_module_ids,
                    'is_more'                        => $is_more,
                    'min_discount'                   => $min_discount,
                    'min_price'                      => $min_price,
                ];
            }
        }

        $data = [
            '_meta' => [
                'total_count'  => $list->total(),
                'page_count'   => $list->lastPage(),
                'current_page' => $list->currentPage(),
                'per_page'     => $list->perPage()
            ],
            "items" => $items
        ];

        return $this->res([
            'data' => $data
        ]);
    }

    public function keywords(Request $request)
    {
        $hotKeywordModel = new HotKeyword();
        $result['hot_keywords'] = $hotKeywordModel->getKeywords();

        $result['user_search_keywords'] = [];
        $list = UserSearchKeyword::query()->select(['keyword','user_id'])->where('user_id',$this->user_id)->orderBy('id','desc')->limit(10)->get();
        if(!empty($list))
        {
            foreach ($list as $v)
            {
                $result['user_search_keywords'][] = [
                    'keyword' => $v->keyword
                ];
            }
        }

        return $this->ok($result);
    }

    public function deleteKeywords()
    {
        UserSearchKeyword::query()->where('user_id',$this->user_id)->delete();
        return $this->ok();
    }

    public function randSell(Request $request)
    {
        $bool = false;
        if(!$bool)
        {
            $row = User::query()->where('user_type',0)->inRandomOrder()->first();

            $first = mb_substr($row->wechat_nickname,0,1);
            $count = mb_strlen($row->wechat_nickname);

            $wechat_nickname = $count <=2 ? $first.'*' : $first.'**';
            $result = [
                'wechat_nickname' => $wechat_nickname,
                'wechat_avatar'   => $row->wechat_avatar,
                'price'           => mt_rand(30,150).'.'.mt_rand(0,9),
            ];
        }
        else
        {

        }


        return $this->ok($result);
    }

}
